package com.example.zhc36.fragment.frag;



import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import android.widget.TextView;

import com.example.zhc36.fragment.MainmoreFragment;
import com.example.zhc36.fragment.R;
import com.example.zhc36.fragment.apadter.RvadapterFragment;
import com.example.zhc36.fragment.bean.Featurebean;
import com.example.zhc36.fragment.dao.FeatureDao;


import com.example.zhc36.fragment.utils.app.BaseFragment;


import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class MaindtFragment extends BaseFragment implements View.OnClickListener {
    private RecyclerView rvv;
    private ImageView img;
    private TextView t2,sousuo,tv;
    private List<Featurebean> findshow;
    private RvadapterFragment fragment;
    private FeatureDao dao;


    protected View initView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
        View view = inflater.inflate(R.layout.frag_main_news, container, false);
        t2=view.findViewById(R.id.tv2);
        sousuo=view.findViewById(R.id.sousuo);
        rvv=view.findViewById(R.id.rev);
        tv=view.findViewById(R.id.tv);
        tv.requestFocus();


        return view;
    }


    @Override
    protected void initData() {
        dao = new FeatureDao();
        findshow = dao.findshow();
        fragment = new RvadapterFragment(findshow);
         rvv.setLayoutManager(new LinearLayoutManager(act,LinearLayoutManager.VERTICAL,false));
        rvv.setAdapter(fragment);
        ItemTouchHelper.Callback callback = new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
                return makeMovementFlags(ItemTouchHelper.UP | ItemTouchHelper.DOWN, ItemTouchHelper.LEFT);
            }

            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
                int from = viewHolder.getLayoutPosition();
                int to = viewHolder1.getLayoutPosition();
//                findshow.add(to,findshow.remove(from));

                Collections.swap(findshow,from,to);
                fragment.notifyItemMoved(from,to);


                return true;
            }

            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
                int layoutPosition = viewHolder.getLayoutPosition();
                findshow.remove(layoutPosition);
                fragment.notifyItemRemoved(layoutPosition);


            }
        };
        new ItemTouchHelper(callback).attachToRecyclerView(rvv);
    }

    @Override
    protected void initListener() {
        t2.setOnClickListener(this);

    }

    @Override
    public void onResume() {
        super.onResume();
        findshow.clear();
        fragment.notifyItemRangeRemoved(0,findshow.size());
        findshow.addAll(dao.findshow());
        fragment.notifyItemRangeInserted(0,findshow.size());




    }

    @Override
    public void onClick(View view) {
        if(view==t2){

       jumpToActivity(MainmoreFragment.class);


    }
    }
}
